package com.coc.service;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class RabbitFanoutListenner {

//    @Autowired
//    private AmqpTemplate amqpTemplate;

    @RabbitListener(queues = "fanout.a")
    public void recievedA (Message message, Channel channel) throws IOException {

        // 采用手动应答模式, 手动确认应答更为安全稳定
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);

        System.out.println( message == null ? "null": "消费者[fanout.a]接收： " + new String(message.getBody()) +"["+ message+"] - - " + message.getMessageProperties().getDeliveryTag() );
    }

    @RabbitListener(queues = "fanout.b")
    public void recievedB(Message message, Channel channel) throws IOException {

        // 采用手动应答模式, 手动确认应答更为安全稳定
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);

        System.out.println( message == null ? "null": "消费者[fanout.b]接收： " + new String(message.getBody()) +"["+ message+"] - - " + message.getMessageProperties().getDeliveryTag() );
    }

    @RabbitListener(queues = "fanout.c")
    public void recievedC (Message message, Channel channel) throws IOException {

        // 采用手动应答模式, 手动确认应答更为安全稳定
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);

        System.out.println( message == null ? "null": "消费者[fanout.c]接收： " + new String(message.getBody()) +"["+ message+"] - - " + message.getMessageProperties().getDeliveryTag() );
    }



}
